    //
//  HostViewController.m
//  TabulaImaginarius
//
//  Created by TA2 on 4/4/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "HostViewController.h"


@implementation HostViewController

@synthesize backButton;

- (void)dealloc {
    [backButton release];
    backButton = nil;
	[newSessionButton release];
    newSessionButton = nil;
    [workInProgressView release];
    workInProgressView = nil;
    [gameName release];
    gameName = nil;
    [dimmingLayerView release];
    dimmingLayerView = nil;
    [currentMapViewController release];
    currentMapViewController = nil;
    [super dealloc];
}

- (void)awakeFromNib {
    currentMapViewController = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewDidAppear:(BOOL)animated {
    if (currentMapViewController) {
        [currentMapViewController release];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [[LoadedImages sharedLoadedImages].savedFiles removeAllObjects];
    NSArray *fileNames;
    if(TARGET_IPHONE_SIMULATOR) {
        fileNames = [[NSBundle mainBundle] pathsForResourcesOfType:nil inDirectory:nil];
    } else {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSFileManager *manager = [NSFileManager defaultManager];
        fileNames = [manager directoryContentsAtPath:documentsDirectory];
    }
	for (NSString *fileName in fileNames) {
		if ([[fileName lastPathComponent]hasSuffix:@".save"]) {
            [[LoadedImages sharedLoadedImages].savedFiles addObject:fileName];
        }
    }
    [sessionList reloadData];
}

- (IBAction)goBack:(id)sender {
	[self dismissModalViewControllerAnimated:YES];  
}

- (IBAction)hostNewSession:(id)sender {
    NSString *sessionName = gameName.text;
    NSString *archivePath;
    if(TARGET_IPHONE_SIMULATOR) {
        archivePath = [[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:sessionName] stringByAppendingString:@".save"];
    } else {
        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        archivePath = [[documentsDirectory stringByAppendingPathComponent:sessionName] stringByAppendingString:@".save"];
    }
    if ([[NSFileManager defaultManager] fileExistsAtPath:archivePath]) {
        int index = 1;
        do {
            sessionName = [gameName.text stringByAppendingFormat:@"(%d)", index];
            if(TARGET_IPHONE_SIMULATOR) {
                archivePath = [[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:sessionName] stringByAppendingString:@".save"];
            } else {
                NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                archivePath = [[documentsDirectory stringByAppendingPathComponent:sessionName] stringByAppendingString:@".save"];
            }
            index++;
        } while ([[NSFileManager defaultManager] fileExistsAtPath:archivePath]);
    }
    [self loadMapWithName:sessionName];
    
}

- (IBAction)deleteSavedFile:(id)sender {
    
}

- (void)loadMapWithName:(NSString *)mapName {
    [self disableUI];
    [[NetworkController sharedNetworkController] startSession];
    dispatch_queue_t mapInitiate = dispatch_queue_create("initiate map", NULL);
    dispatch_async(mapInitiate, ^{
        currentMapViewController = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil withMapName:mapName];
        [NetworkController sharedNetworkController].bonjourIsActive = NO;
        [self presentModalViewController:currentMapViewController animated:YES];
        [self enableUI];
    });
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}

- (void)disableUI {
	[workInProgressView startAnimating];
	dimmingLayerView.alpha = 0.3;
	newSessionButton.enabled = NO;
	backButton.enabled = NO;
	gameName.enabled = NO;
}

- (void)enableUI {
	[workInProgressView stopAnimating];
	dimmingLayerView.alpha = 0.0;
	newSessionButton.enabled = YES;
	backButton.enabled = YES;
	gameName.enabled = YES;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

@end
